public class Solution {
    public int trap(int[] height) {
        if(height == null || height.length == 0)
            return 0;
        int max = 0;
        int res = 0;
        int[] container = new int[height.length];
        for(int i = 0; i < height.length; i++) {
            container[i] = max;
            max = Math.max(max, height[i]);
        }
        max = 0;
        for(int i = height.length - 1; i >= 0; i--) {
            container[i] = Math.min(max, container[i]);
            max = Math.max(max, height[i]);
            res += container[i] - height[i] > 0 ? container[i] - height[i] : 0;
        }
        return res;
    }
}